package jsyf.oa.web.psi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.psi.GoodsFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Maqiuqiu on 2018/10/17.
 */
@Api(value = "进销存-商品接口", description = "进销存-商品接口")
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsFacade goodsFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.goodsFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = goodsFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = goodsFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getGoodsList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询商品列表",
            notes = "查询商品列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getGoodsList(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.goodsFacade.getGoodsList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getGoodsById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据id获取商品信息",
            notes = "根据id获取商品信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:''}",dataType = "String", paramType = "body")
    public void getGoodsById(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.goodsFacade.getGoodsById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getInventoryGoodsInfoByGoodsId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据id获取库存商品信息",
            notes = "根据id获取库存商品信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:''}",dataType = "String", paramType = "body")
    public void getInventoryGoodsInfoByGoodsId(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.goodsFacade.getInventoryGoodsInfoByGoodsId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getGoodsInfoByName.json", method = RequestMethod.POST)
    public void getGoodsInfoByName(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = this.goodsFacade.getGoodsInfoByName(jsonData);
        if (null != result && result.size() > 0) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
}
